﻿Imports Licenta3.SetariClassViewModel
Imports Licenta3.SetariClass
Imports System.Net
Imports System.IO
Imports Licenta3.Common.Classes

Public Class TSetariForm

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        'Me.DataContext = setari
        'Me.DataContext = setariVM
    End Sub
    Private setariVM As New SetariClassViewModel

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        setariVM.setarile.lAutorizat.Add(item:=New TAutorizat)
        autorizatiListView.SelectedIndex = setariVM.setarile.lAutorizat.Count - 1
        autorizatiListView.ScrollIntoView(autorizatiListView.SelectedItem)
        'DataContext = setariVM
    End Sub


    Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
        Me.DialogResult = True
    End Sub

    Private Sub MenuItem_Click(sender As Object, e As RoutedEventArgs)
        setariVM.setarile.lAutorizat.RemoveAt(autorizatiListView.Items.IndexOf(autorizatiListView.SelectedItem))
    End Sub

    Private Sub Button_Click_2(sender As Object, e As RoutedEventArgs)
        Dim dialog As New System.Windows.Forms.FolderBrowserDialog

        dialog.ShowDialog()
        setariVM.setarile.LucruPath = dialog.SelectedPath
        'SetariClass._LucruPath = dialog.SelectedPath
    End Sub

    Private Sub Button_Click_3(sender As Object, e As RoutedEventArgs)
        'actualizeaza lista OCPI
        'MsgBox("Se actualizeaza. Asteptati mesajul de confirmare. Poate dura si 1 minut!")
        Try
            Dim DWD As New WebClient
            Dim countiesURI As String = "http://ancpi.ro/CerereRGI/AT_getCounties.php?sid="
            Dim BCPIURIBase As String = "http://ancpi.ro/CerereRGI/AT_getOrgUnits.php?id="
            Dim BCPIURI As String
            Dim rand As Integer
            rand = Math.Round(Rnd() * 100)
            countiesURI = countiesURI + Math.Round(Rnd() * 100).ToString
            Dim countiesStream As New StreamReader(DWD.OpenRead(countiesURI))
            Dim BCPIStream As StreamReader
            Dim OCPIlinie As String
            Dim BCPIlinie As String
            Dim curOCPI As Dictionar.OCPIClass
            Dim curBCPI As Dictionar.OCPIClass.BCPIClass
            If SetariClassViewModel._setarile.lists.OCPI IsNot Nothing Then
                SetariClassViewModel._setarile.lists.OCPI.Clear()
            End If

            While Not countiesStream.EndOfStream
                OCPIlinie = countiesStream.ReadLine()
                If OCPIlinie.Contains("<option value=""") Then
                    curOCPI = New Dictionar.OCPIClass
                    extractCodDenumire(OCPIlinie, curOCPI.cod, curOCPI.denumire)
                    BCPIURI = BCPIURIBase + curOCPI.cod + "&sid=" + Math.Round(Rnd() * 100).ToString
                    BCPIStream = New StreamReader(DWD.OpenRead(BCPIURI))
                    While Not BCPIStream.EndOfStream
                        BCPIlinie = BCPIStream.ReadLine
                        If BCPIlinie.Contains("<option value=""") Then
                            curBCPI = New Dictionar.OCPIClass.BCPIClass
                            extractCodDenumire(BCPIlinie, curBCPI.cod, curBCPI.denumire)
                            curOCPI.BCPI.Add(curBCPI)
                        End If
                    End While
                    setariVM.setarile.lists.OCPI.Add(curOCPI)
                End If
            End While
            If MsgBox("Lista OCPI/BCPI s-a actualizat") = MsgBoxResult.Ok Then
                actualizare.Text = ""
            End If
        Catch ex As Exception
            MsgBox(ex.Message + ex.StackTrace + ex.Source)
        End Try

    End Sub
End Class
